<?php
namespace common\bonus;
use common\models\TradeModel;
use common\utils\CommonFun;
use common\models\TeamTotal;
use common\bonus\Validata;
use common\models\Member;
/*
	用户升级
*/
class Upgrade
{

	public static function bonusAction($params)
	{
		$conf = TradeModel::getUpgradeConf('Upgrade');
		if(!$conf) return ['state' => false,'msg' => '参数配置有误'];

		//触发会员自身的升级
		if(count($conf) <=1 ){
			self::selfUpgrade($conf['0'],$params);
		}else{
			self::teamUp($conf,$params);
		}
		return ['state' => true,'msg' => 'success'];
	}


	## 团队升级 (也包括自己)
	protected static function teamUp($conf,$params)
	{
		foreach($params['uper'] as $k=>$member)
		{
			$newGrade = self::getNewGrade($member,$conf);
			if($k == 0){
				$upData = ['update_time'=>time()];

				if($newGrade > $member['grade']) $upData['grade'] = $newGrade;
			}else
			{
				if($newGrade <= $member['grade']) continue;
				$upData = ['grade' => $newGrade,'update_time'=>time()];
			}
			//升级
			$res = Member::updateAll($upData,['member_id'=>$member['member_id']]);
			if(!$res) CommonFun::writeLog('用户升级失败，member_id:'.$member['member_id'],'bonus');
		}
		return true;
	}



	## 自己升级
	protected static function selfUpgrade($conf,$params)
	{
		if($params['member']['grade'] >= $conf['grade']) return true;
		$conditionArr = explode(',', $conf['model_condition']);

		//验证投资金额升级
		if(in_array('invest_money',$conditionArr))
		{
			$data = TeamTotal::find()
					->select(['sum(my_total) as total'])
					->where(['member_id' => $params['member_id'],'mch_id'=>MCHID])
					->asarray()->one();

			$ruleData = array();
			$ruleData['rule'] = $conf['condition'];
			$ruleData['context']['invest_money'] = isset($data['total'])?$data['total']:0;
			if(!Validata::validata($ruleData)) return true;
		}

		//升级
		return Member::updateAll(['grade' => $conf['grade'],'update_time'=>time()],['member_id'=>$params['member_id']]);
	}


	## 获取可升级的等级
	protected static function getNewGrade($member,$conf)
	{
		$conditionArr = explode(',', $conf['0']['model_condition']);
		$ruleData = array();

		## 自己的投资金额 
		if(in_array('invest_money',$conditionArr))
		{
			$data = TeamTotal::find()
					->select(['sum(my_total) as total'])
					->where(['member_id'=>$member['member_id'],'mch_id'=>MCHID])
					->asarray()->one();
			$ruleData['context']['invest_money'] = isset($data['total'])?$data['total']:0;
		}	

		## 直推的有效会员
		if(in_array('child_num',$conditionArr))
		{
			$childNum = Member::find()->where(['invite_id'=>$member['member_id']])->andWhere(['>','grade',0])->count();
			$ruleData['context']['child_num'] = $childNum;
		}

		## 团队业绩
		if(in_array('team_total',$conditionArr))
		{
			$data = TeamTotal::find()
					->select(['sum(team_total) as total'])
					->where(['member_id'=>$member['member_id'],'mch_id'=>MCHID])
					->asarray()->one();
			$ruleData['context']['team_total'] = isset($data['total'])?$data['total']:0;
		}

		//遍历规则
		$newGrade = 0;
		foreach($conf as $val)
		{
			$ruleData['rule'] = $val['condition'];

			if(Validata::validata($ruleData)){ 
				$newGrade = $val['grade'];
			} 
		}
		return $newGrade;
	}

}

